<?php

namespace App\Remote\Orm;

use App\Enums\Check\CheckEnum;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use setasign\Fpdi\Tfpdf\FpdfTpl;

class OrmNewOrderRemote extends BaseOrmRemote
{
    const NEW_ORDER_STATUS_WAIT   = 1; // 未支付
    const NEW_ORDER_STATUS_ING    = 2; // 进行中
    const NEW_ORDER_STATUS_FINISH = 3; // 已完成
    const NEW_ORDER_STATUS_CLOSE  = 4; // 已关闭
    const NEW_ORDER_STATUS_PART   = 5; // 分笔支付中

    // 开票状态
    const INVOICE_STATUS_NOT         = 0;// 未开票
    const INVOICE_STATUS_ING         = 1;// 开票中
    const INVOICE_STATUS_ERROR       = 2;// 开票失败
    const INVOICE_STATUS_SUCCESS     = 3;// 开票成功
    const INVOICE_STATUS_RED_ING     = 4;// 红冲中
    const INVOICE_STATUS_RED_SUCCESS = 5;// 红冲成功

    const SUPPLIER_TYPE_SERVICE = 5; // 供应商 - 服务类


    /**
     * 门店首批订货单、出库单列表
     * @param int $shopId 店铺id
     * @return array|false|null
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function newOrderFirstDeliveryNotes(int $shopId)
    {
        return self::request(
            "GET",
            'internal/newOrder/firstDeliveryNotes',
            ['shopId' => $shopId, 'brandId' => 1],
            null,
            'query'
        );
    }

    /**
     * 门店店铺筹备设备出库单
     * @param int $shopId
     * @param string $date
     * @return array|false|null
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function equipmentDeliveryNotes(int $shopId, string $date, int $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT)
    {
        return self::request(
            "GET",
            'internal/newOrder/equipmentDeliveryNotes',
            ['shopId' => $shopId, 'date' => $date, 'brandId' => 1, 'sysType' => $sysType],
            null,
            'query'
        );
    }

    /**
     * 订单取消
     * @param int $id
     * @param string $username
     * @return array|false|null
     */
    public static function cancelNewOrder(int $id, string $username)
    {
        return self::request(
            "POST",
            'internal/newOrder/cancel',
            ['id' => $id, 'username' => $username],
            null,
            'json'
        );
    }

    public static function deliveryNoteReceived(int $deliveryNoteId)
    {
        return self::request(
            "POST",
            'internal/newOrder/deliveryNoteReceived',
            ['id' => $deliveryNoteId],
            null,
            'json'
        );
    }

    public static function myEquipments(int $shopId, int $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT)
    {
        return self::request(
            "GET",
            'internal/newOrder/myEquipments',
            ['shopId' => $shopId, 'brandId' => 1, 'sys_type' => $sysType],
            null,
            'query'
        );
    }

    public static function equipmentsByNewOrder(int $id, int $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT)
    {
        return self::request(
            "GET",
            'internal/newOrder/equipmentsByNewOrder',
            ['id' => $id, 'sys_type' => $sysType],
            null,
            'query'
        );
    }

    public static function deliveryNoteDetail(int $deliveryNoteId, int $shopId)
    {
        return self::request(
            "GET",
            'internal/newOrder/deliveryNoteDetail',
            ['id' => $deliveryNoteId, 'shopId' => $shopId],
            null,
            'query'
        );
    }

    /**
     * 添加出库单物流信息
     * @param int $deliveryNoteId
     * @param array $list
     * @return mixed
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function deliveryNoteLogistics(int $deliveryNoteId, array $list)
    {
        return self::request(
            "POST",
            'internal/newOrder/deliveryNoteLogistics',
            ['id' => $deliveryNoteId, 'list' => $list],
            null,
            'json'
        );
    }

    /**
     * app用的
     * @param int $shopId
     * @param string $date
     * @return array
     */
    public static function merchantAppLogistics(int $shopId, string $date)
    {
        return self::request(
            "GET",
            'internal/newOrder/merchantAppLogistics',
            ['shopId' => $shopId, 'date' => $date],
            null,
            'query'
        );
    }

    public static function createEquipmentNewOrder(int $shopId, array $equipments, string $operatorName, int $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT): array
    {
        return self::request(
            "POST",
            'internal/newOrder/createEquipmentNewOrder',
            ['shopId' => $shopId, 'equipments' => $equipments, 'operator_name' => $operatorName, 'sys_type' => $sysType],
            null,
            'json'
        );
    }

    public static function updateEquipmentNewOrder(int $shopId, array $equipments, string $operatorName, int $newOrderId): array
    {
        return self::request(
            "POST",
            'internal/newOrder/updateEquipmentNewOrder',
            ['shopId' => $shopId, 'equipments' => $equipments, 'operator_name' => $operatorName, 'newOrderId' => $newOrderId],
            null,
            'json'
        );
    }

    public static function getNewOrderByShopId(int $shopId, int $days = -30, string $sysType = '', string $status = '', string $invoicingStatus = '', string $hasServiceSupplier = ''): array
    {
        return self::request(
            "GET",
            'internal/newOrder/getNewOrderByShopId',
            [
                'shopId'        => $shopId,
                'days'          => $days,
                'status'        => $status,         // 订单状态
                'sysType'       => $sysType,        // 类型，2-商家app订货，6-开店筹备设备费
                'invoiceStatus' => $invoicingStatus,// 开票状态 0-未开票
                'hasServiceSupplier' => $hasServiceSupplier, // 是否有服务类商品，(空)-全部，1-必须有服务类商品，0-必须没有服务类商品
            ],
            null,
            'query'
        );
    }

    public static function getNewOrderGoodsByOrmOrder(string $ormOrder): array
    {
        return self::request(
            "GET",
            'internal/newOrder/getNewOrderGoodsByOrmOrder',
            ['ormOrder' => $ormOrder],
            null,
            'query'
        );
    }

    public static function getDeliveryAddressByShopIdByShopId(int $shopId): array
    {
        return self::request(
            "GET",
            'internal/store/getDeliveryAddressByShopId',
            ['shopId' => $shopId],
            null,
            'query'
        );
    }

    public static function getOrderTargetByMonth(string $month, int $shopId): array
    {
        return self::request(
            "GET",
            'internal/newOrder/getNewOrderByMonth',
                [
                    'month'  => $month,
                    'shopId' => $shopId
                ],
            null,
            'query'
        );
    }

    public static function getFirstBookTime(int $shopId)
    {
        return self::request(
            "GET",
            'internal/newOrder/getFirstBookTime',
            [
                'shopId' => $shopId
            ],
            null,
            'query'
        );
    }

    /**
     * 订货订单详情页 id、code 二选一
     * @param int $id
     * @param string $code
     * @return array|false|null
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function detail(int $id = 0, string $code = '')
    {
        return self::request(
            'GET',
            'internal/newOrder/detail',
            compact('id', 'code'),
            null,
            'query'
        );
    }


    /**
     * 获取供应商
     * @param int $supplier_type
     * @return array
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function getOrmSupplier(int $supplier_type = 0): array
    {
        return self::request(
            "GET",
            'internal/supplier/index',
            compact('supplier_type'),
            null,
            'query'
        );
    }

    /**
     * 查询一段时间内订货单数据
     * @param string $startTime
     * @param string $endTime
     * @param int $shopId
     * @return array
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function getOrderListByShopId(string $startTime,string $endTime, int $shopId): array
    {
        return self::request(
            "GET",
            'internal/newOrder/accountIndex',
            [
                'start_create_time' => $startTime,
                'end_create_time'   => $endTime,
                'shop_id'           => $shopId
            ],
            null,
            'query'
        );
    }

    /**
     * 工服配置
     * @return array
     */
    public static function getWorkClothesGroups(): array
    {
        return self::request(
            "GET",
            'internal/goods/workClothesGroups',
            [],
            null,
            'query'
        );
    }

    /**
     * 创建工服订单
     * @param int $shop_id
     * @param int $supplier_id
     * @param array $goods
     * @return array
     */
    public static function createWorkClothes(int $shop_id, int $supplier_id, array $goods, int $uid, string $username): array
    {
        return self::request(
            "POST",
            'internal/newOrder/workClothesCreate',
            [
                'shop_id'     => $shop_id,
                'supplier_id' => $supplier_id,
                'goods'       => $goods,
                'uid'         => $uid,
                'username'    => $username,
            ],
            null,
            'json'
        );
    }

    /**
     * 核销工服订单
     * @param int $new_order_id
     */
    public static function writeOffWorkClothes(int $new_order_id)
    {
        return self::request(
            "POST",
            'internal/newOrder/workClothesWriteOff',
            [
                'new_order_id' => $new_order_id
            ],
            null,
            'json'
        );
    }

    /**
     * 关闭工服订单
     * @param int $new_order_id
     */
    public static function closeWorkClothes(int $new_order_id, int $uid, string $username, int $sys_type)
    {
        return self::request(
            "POST",
            'internal/newOrder/workClothesClose',
            [
                'new_order_id' => $new_order_id,
                'uid'          => $uid,
                'username'     => $username,
                'sys_type'     => $sys_type,
            ],
            null,
            'json'
        );
    }

    /**
     * 批量关闭活动订单
     * @param array $ids 订货单id
     * @param int $uid
     * @param string $username
     * @param string $reason
     * @return array|false|null
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public static function batchCloseOrder(array $ids, int $uid, string $username, string $reason = '活动截止，自动关闭')
    {
        return self::request(
            'POST',
            '/internal/newOrder/batchClose',
            [
                'ids'      => $ids,
                'uid'      => $uid,
                'username' => $username,
                'reason'   => $reason,
            ],
            null,
            'json'
        );
    }
}
